package com.example.demo.service.aliyun.message;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.example.demo.config.MessageConfig;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 这个类用来发送阿里云的第三方短信
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class messageTools {

    @NonNull
    private MessageConfig messageConfig;

     private  SendSmsResponse sendSms(String telephone,String templateCode,String templateContent) throws ClientException {

        // 可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout",messageConfig.getDefaultConnectTimeout());
        System.setProperty("sun.net.client.defaultReadTimeout", messageConfig.getDefaultReadTimeout());

        // 初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile(messageConfig.getRegionId(), messageConfig.getAccessKeyId(), messageConfig.getAccessKeySecret());
        DefaultProfile.addEndpoint(messageConfig.getRegionId(), messageConfig.getRegionId(), messageConfig.getProductName(), messageConfig.getDomain());
        IAcsClient acsClient = new DefaultAcsClient(profile);

        // 组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        // 必填:待发送手机号
        request.setPhoneNumbers(telephone);
        // 必填:短信签名-可在短信控制台中找到
        request.setSignName(messageConfig.getSignName()); // TODO 改这里
        // 必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);  // TODO 改这里
        // 可选:模板中的变量替换JSON串,如模板内容为"亲爱的用户,您的验证码为${code}"时,此处的值为
        request.setTemplateParam(templateContent);

        // 选填-上行短信扩展码(无特殊需求用户请忽略此字段)
        // request.setSmsUpExtendCode("90997");

        // 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
        request.setOutId("yourOutId");

        // hint 此处可能会抛出异常，注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
            log.info("短信发送成功");
        }else {
            log.warn("短信发送失败telephone=%s,templateCode=%s,templateContent=%s",telephone,templateCode,templateContent);
        }
        return sendSmsResponse;
    }

    public void sendCode(String phone,String templateCode,String templateContent){
        try {
            SendSmsResponse sendSms =sendSms(phone,"SMS_204126457",templateContent);
            log.info(sendSms.getCode());
            log.info(sendSms.getMessage());
            log.info(sendSms.getRequestId());
            log.info(sendSms.getBizId());
        }catch (Exception e){
            e.printStackTrace();
            log.error("短信发送失败phone={},templateCode={},templateContent={},msg={}",phone,templateCode,templateContent,e.getCause());
        }
    }
}
